from django.utils.datetime_safe import datetime
from rest_framework import serializers
from xyz.models import User, City, Enterprise, Recruit, Spit


class UserModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        # fields = '__all__'        # 最简单 最偷懒的方法
        fields = ['id', 'username', 'mobile', 'password', 'avatar']

        # password 应该只在 反序列化的时候使用
        extra_kwargs = {
            'password': {
                'write_only': True,
                'min_length': 8,
                'max_length': 20
            },
            'avatar': {'default': None}
        }

    def create(self, validated_data):
        return User.objects.create_user(**validated_data)


class HotCitySerializer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields = ['id', 'name', 'ishot']


class HotEnterpriseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Enterprise
        fields = ['id', 'name', 'labels', 'logo', 'recruits', 'summary']


class RecruitsSerializer(serializers.ModelSerializer):
    enterprise = HotEnterpriseSerializer()

    class Meta:
        model = Recruit
        fields = ['id', 'jobname', 'salary',
                  'condition', 'type', 'city',
                  'createtime', 'enterprise', 'labels']


class SpitModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Spit
        fields = '__all__'

        extra_kwargs = {
            'collected': {
                'write_only': True,
                'default': 0
            },
            'hasthumbup': {
                'write_only': True,
                'default': 0
            },
                'userid':{
                    'default': None
                },
                'nickname':{
                    'default':'匿名'
                },
                'visits':{
                    'default':0
                },
                'thumbup':{
                    'default':0
                },
                'avatar':{
                    'default':None
                },
                # 'publishtime': {
                #     'default': datetime.now()
                # },

        }
